<?php 
    class DormitoryAllocatingBLL{
        //获取公寓宿舍列表（按公寓和楼层获取）
        public function getApartmentDormitoryList($apartmentID,$floor,$page,$pageSize){
            $action=0;
            
            if($floor>0){
                $action=1;
            }
            $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
            $result=$dormitoryAllocatingDAL->getApartmentDormitoryList($action,$apartmentID,$floor,$page*$pageSize,$pageSize);
            
            return $result;
        }

        //获取公寓宿舍列表页数
        public function getApartmentDormitoryListPage($apartmentID,$floor,$pageSize){
            $pageMax=0;
            $count=0;
            $result=null;

            $action=0;
            if($floor>0){
                $action=1;
            }
            $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
            $result=$dormitoryAllocatingDAL->getApartmentDormitoryListPage($action,$apartmentID,$floor,$pageSize);
            
            if($result!=null){
                $count=$result['count'];
            }
            if($count>$pageSize){
                if($count%$pageSize===0){
                    $pageMax=(int)($count/$pageSize);
                }else{
                    $pageMax=(int)($count/$pageSize)+1;
                }
            }

            return $pageMax;
        }

        //获取辅导员所管理的宿舍
        public function getInstructorDormitoryList($instructorID,$grade,$major,$property,$apartmentID,$floor,$page,$pageSize){
            $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
            $action=$this->getInstructorDormitoryListAction($grade,$major,$property,$apartmentID,$floor);
            $result=$dormitoryAllocatingDAL->getInstructorDormitoryList($action,$instructorID,$grade,$major,$property,$apartmentID,$floor,$page*$pageSize,$pageSize);

            return $result;
        }

        //获取辅导员宿舍列表页数
        public function getInstructorDormitoryListPage($instructorID,$grade,$major,$property,$apartmentID,$floor,$pageSize){
            $pageMax=0;
            $count=0;
            $result=null;

            $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
            $action=$this->getInstructorDormitoryListAction($grade,$major,$property,$apartmentID,$floor);
            $result=$dormitoryAllocatingDAL->getInstructorDormitoryListPage($action,$instructorID,$grade,$major,$property,$apartmentID,$floor);
            
            if($result!=null){
                $count=$result['count'];
            }
            if($count>$pageSize){
                if($count%$pageSize===0){
                    $pageMax=(int)($count/$pageSize);
                }else{
                    $pageMax=(int)($count/$pageSize)+1;
                }
            }

            return $pageMax;
        }

        /**
         * action
        * 1-查询所有，查询所有公寓、楼层
        * 2-查询所有，按公寓查询
        * 3-查询所有，按楼层查询
        * 4-查询所有，按公寓、楼层查询
        * 5-分类查询，查询所有公寓、楼层
        * 6-分类查询，查询指定公寓
        * 7-分类查询，查询指定楼层
        * 8-分类查询，查询指定公寓、楼层
        */
        //获取搜索列表
        public function getSearchList($select,$instructorID,$grade,$major,$property,$apartmentID,$floor,$search,$page,$pageSize){
            switch((int)$select){
                //查找所有
                case 0:
                    $action=$this->getSearchAction($property,$apartmentID,$floor);
                    $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
                    $result=$dormitoryAllocatingDAL->getSearchApartmentDormitoryList($action,$property,$apartmentID,$floor,$search,$page,$pageSize);
                    break;
                //查找已分配
                case 1:
                    $action=$this->getSearchAction($property,$apartmentID,$floor);
                    $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
                    $result=$dormitoryAllocatingDAL->getSearchInstructorDormitoryList($action,$instructorID,$grade,$major,$property,$apartmentID,$floor,$search,$page,$pageSize);
                    break;
                default:break;
            }

            return $result;
        }
        //获取搜索页数
        public function getSearchListPage($select,$instructorID,$grade,$major,$property,$apartmentID,$floor,$search,$pageSize){
            $count=0;
            $result=null;
            $pageMax=0;
            switch((int)$select){
                //查找所有
                case 0:
                    $action=$this->getSearchAction($property,$apartmentID,$floor);
                    $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
                    $result=$dormitoryAllocatingDAL->getSearchApartmentDormitoryListPage($action,$property,$apartmentID,$floor,$search);
                    break;
                //查找已分配
                case 1:
                    $action=$this->getSearchAction($property,$apartmentID,$floor);
                    $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
                    $result=$dormitoryAllocatingDAL->getSearchInstructorDormitoryListPage($action,$instructorID,$grade,$major,$property,$apartmentID,$floor,$search);
                    break;
                default:break;
            }
            if($result!=null){
                $count=$result['count'];
            }
            if($count>$pageSize){
                if($count%$pageSize===0){
                    $pageMax=(int)($count/$pageSize);
                }else{
                    $pageMax=(int)($count/$pageSize)+1;
                }
            }

            return $pageMax;
        }

        //获取查询模式
        public function getInstructorDormitoryListAction($grade,$major,$property,$apartmentID,$floor){
            /**
             * 1-查询所有。
             * 2-查询所有，按年级查询。
             * 3-查询所有，按专业查询。
             * 4-查询所有，按年级、专业查询
             * 5-查询指定性别。
             * 6-查询所有性别，按年级查询。
             * 7-查询所有性别，按专业查询。
             * 8-查询所有性别，按年级、专业查询。
             * 9-查询所有,按公寓查询
             * 10-查询所有,按公寓、年级查询
             * 11-查询所有,按公寓、专业查询
             * 12-查询所有,按公寓、年级、专业查询
             * 13-查询所有,按公寓、楼层查询
             * 14-查询所有,按公寓、楼层、年级查询
             * 15-查询所有,按公寓、楼层、专业查询
             * 16-查询所有,按公寓、楼层、年级、专业查询
             */
            $action=0;

            if($apartmentID==0&&$floor==0){
                if($property==0){
                    if($grade==0&&$major==0){
                        $action=1;
                    }else if($major==0){
                        $action=2;
                    }else if($grade==0){
                        $action=3;
                    }else{
                        $action=4;
                    }
                }else{
                    if($grade==0&&$major==0){
                        $action=5;
                    }else if($major==0){
                        $action=6;
                    }else if($grade==0){
                        $action=7;
                    }else{
                        $action=8;
                    }
                }
            }else{
                if($property==0&&$floor==0){
                    if($grade==0&&$major==0){
                        $action=9;
                    }else if($major==0){
                        $action=10;
                    }else if($grade==0){
                        $action==11;
                    }else{
                        $action=12;
                    }
                }else{
                    if($grade==0&&$major==0){
                        $action=13;
                    }else if($major==0){
                        $action=14;
                    }else if($grade==0){
                        $action=15;
                    }else{
                        $action=16;
                    }
                }
            }

            return $action;
        }

        //search查询模式
        public function getSearchAction($property,$apartmentID,$floor){
            /**
             * 1-查询所有
             * 2-查询所有，按公寓查询
             * 3-查询所有，按楼层查询
             * 4-查询所有，按公寓、楼层查询
             * 5-分类查询，查询所有公寓、楼层
             * 6-分类查询，查询指定公寓
             * 7-分类查询，查询指定楼层
             * 8-分类查询，查询指定公寓、楼层
             */
            $action=0;
            if($property==0){
                if($apartmentID==0 && $floor==0){
                    $action=1;
                }else if($floor==0){
                    $action=2;
                }else if($apartmentID==0){
                    $action=3;
                }else{
                    $action=4;
                }
            }else{
                if($apartmentID==0 && $floor==0){
                    $action=5;
                }else if($floor==0){
                    $action=6;
                }else if($apartmentID==0){
                    $action=7;
                }else{
                    $action=8;
                }
            }

            return $action;
        }

        //获取已使用的床位数量
        public function getBedsAlreadyUsedQuantity($apartmentID,$floor,$dormitoryID,$grade,$major){
            $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
            $result=$dormitoryAllocatingDAL->getBedsAlreadyUsedQuantity($apartmentID,$floor,$dormitoryID,$grade,$major);
            if($result['count']==null){
                $count=0;
            }else{
                $count=$result['count'];
            }
            return $count;
        }

        //获取辅导员已使用的床位数量
        public function getInstructorBedsAlreadyUsedQuantity($apartmentID,$floor,$dormitoryID,$uid,$grade,$major){
            $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
            $result=$dormitoryAllocatingDAL->getInstructorBedsAlreadyUsedQuantity($apartmentID,$floor,$dormitoryID,$uid,$grade,$major);
            if($result['beds']==null){
                $count=0;
            }else{
                $count=$result['beds'];
            }
            return (int)$count;
        }
        
        //分配宿舍
        public function instructorAllocatingDormitories($dormitoryList){
            $message=array();
            $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
            //判断宿舍是否已分配给该辅导员
            foreach($dormitoryList as $dormitory){
                $flag=$this->isInstructorAllocatingDormitory($dormitory->apartmentID,$dormitory->floor,$dormitory->dormitoryID,$dormitory->instructorID,$dormitory->grade,$dormitory->major);
                if($flag){
                    $dormitory->action=2;
                }else{
                    $dormitory->action=1;
                }
            }
            //判断宿舍是否已分配
            foreach($dormitoryList as $dormitory){
                //$this->isAllocatingDormitory($dormitory->apartmentID,$dormitory->dormitoryID,$dormitory->instructorID);
            }
            $result=$dormitoryAllocatingDAL->instructorAllocatingDormitories($dormitoryList);
            if($result){
                $message=array(
                    'code'=>'2001',
                    'message'=>'分配成功'
                );
            }else{
                $message=array(
                    'code'=>'2004',
                    'message'=>'分配失败'
                );
            }

            return $message;
        }

        //移除分配的宿舍
        public function instructorAllocatingDormitoriesRemove($dormitoryList){
            $message=array();
            $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
            $result=$dormitoryAllocatingDAL->instructorAllocatingDormitoriesRemove($dormitoryList);
            if($result){
                $message=array(
                    'code'=>'2001',
                    'message'=>'移除成功'
                );
            }else{
                $message=array(
                    'code'=>'2004',
                    'message'=>'移除失败'
                );
            }

            return $message;
        }

        //是否已分配该宿舍，按公寓、楼层、宿舍、辅导员、年级、专业查找
        public function isInstructorAllocatingDormitory($apartmentID,$floor,$dormitoryID,$uid,$grade,$major){
            $flag=false;
            $dormitoryAllocatingDAL=new DormitoryAllocatingDAL();
            (int)$result=$dormitoryAllocatingDAL->isAllocatingDormitory($apartmentID,$floor,$dormitoryID,$uid,$grade,$major);
            if($result['count']>0){
                $flag=true;
            }

            return $flag;
        }
        //判断宿舍是否已分配
        public function isAllocatingDormitory($apartmentID,$dormitoryID,$instructorID){

        }
    }
?>